package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"shops/models"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) Login() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	var user models.User
	user.UserAccount = c.GetString("account")
	password := c.GetString("password")
	if user.GetUserByAccount() != nil {
		c.Data["json"] = map[string]any{
			"Code": 501,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	if user.ID == 0 {
		c.Data["json"] = map[string]any{
			"Code": 502,
			"Msg":  "账号不存在",
		}
		c.ServeJSON()
		return
	}
	if user.UserPassword != password {
		c.Data["json"] = map[string]any{
			"Code": 503,
			"Msg":  "密码错误",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "登陆成功",
		"Data": user,
	}
	c.ServeJSON()
}
